//
//  ChooseActivityVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/10/24.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ChooseActivityVC.h"
#import "MyActityCell.h"
@interface ChooseActivityVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ChooseActivityVC
{
    UITableView *_tableView;
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSArray *_responseArr;
    
    NSIndexPath *_currentPath;
    NSString *_activityName;
    NSString *_IdStr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = @"选择和游记相关的活动";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
    [self requsetForChooseActityList];
    [self initTableView];
}
#pragma - mark RequetForActityList
- (void)requsetForChooseActityList{
    //默认参数
    NSDictionary *_parameters = @{
                    @"userId": [NSString readToNSDefaultsWithKey:kId],
//                    @"status": @"1",
                    @"pageNo": @"1",
                    @"pageSize": @"10",
                    };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/activity/listWithUserID.action" parameters:_parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求活动列表失败"];
    }];
}

- (void)initTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
#pragma - mark UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyActityCell *cell = [[NSBundle mainBundle]loadNibNamed:@"MyActityCell" owner:nil options:nil][0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLb.text = _responseDic[@"list"][indexPath.row][@"title"];
    cell.cityLb.text = _responseDic[@"list"][indexPath.row][@"destination"];
    NSString *timeStr = _responseDic[@"list"][indexPath.row][@"stratTime"];
    timeStr = [timeStr substringToIndex:10];
    cell.timeLb.text = timeStr;
    NSString *iconUrl = _responseDic[@"list"][indexPath.row][@"coverImgURL"];
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageNamed:@"default"]];
    cell.image.image = [UIImage imageNamed:@" "];
    
    //
    cell.tintColor = Theme_Color;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSInteger row = [indexPath row];
    NSInteger oldRow = [_currentPath row];
    if (row == oldRow && _currentPath!=nil) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger newRow = [indexPath row];
    NSInteger oldRow = (_currentPath !=nil)?[_currentPath row]:-1;
    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_currentPath];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        _currentPath = indexPath;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //
    _activityName = _responseDic[@"list"][indexPath.row][@"title"];
     NSNumber *IdNum = _responseDic[@"list"][indexPath.row][@"id"];
    _IdStr = IdNum.stringValue;
}
- (void)saveAction{
    NSString *buttonTitle = [NSString stringWithFormat:@"%@%@",@"选择的活动为：",_activityName];
    [_IdStr saveToNSDefaultsWithKey:kPAID];
    [self.button setTitle:buttonTitle forState:UIControlStateNormal];
    [[self navigationController] popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
